package com.itheima.controller;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;

    //分页查询
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        log.info("检查项分页查询:{},{},{}", queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        try {
            return checkItemService.findPage(queryPageBean);
        }
        catch (Exception e){
            log.error("检查项分页查询失败: {}", e.getMessage(), e);
            throw new RuntimeException("检查项分页查询失败，请稍后重试");
        }
    }

    //新增检查项
    @PostMapping("/add")
    public Result addCheckItem(@RequestBody CheckItem checkItem) {
        log.info("新增检查项:{}", checkItem);
        try {
            checkItemService.addCheckItem(checkItem);
            return new Result(true, "新增检查项成功");
        }
        catch (Exception e){
            log.error("新增检查项失败: {}", e.getMessage(), e);
            throw new RuntimeException("新增检查项失败");
        }
    }

    //GET /checkitem/findAll
    //删除检查项时要判断检查项是否被检查组关联 如果是则不能删除
    @GetMapping("/delete")
    public Result deleteById(@RequestParam("id") Integer id){
        if (checkItemService.findById(id) != null){
            return new Result(false, "检查项被检查组关联，不能删除");
        }
        if (checkItemService.findById(id) == null){
            return new Result(false, "检查项不存在");
        }
        log.info("根据id删除检查项:{}", id);
        checkItemService.deleteById(id);
        return new Result(true, "删除检查项成功");
    }

    //GET /checkitem/findById
    //根据id查询检查项
    @GetMapping("/findById")
    public Result findById(@RequestParam("id") Integer id){
        log.info("根据id查询检查项:{}", id);
        try{
            CheckItem checkItem = checkItemService.findById(id);
            return new Result(true, "查询成功", checkItem);
        }
        catch (Exception e){
            log.error("根据id查询检查项失败: {}", e.getMessage(), e);
            throw new RuntimeException("根据id查询检查项失败，请稍后重试");
        }
    }


    //POST /checkitem/edit
    //编辑检查项
    @PostMapping("/edit")
    public Result editEdit(@RequestBody CheckItem checkItem) {
        log.info("编辑检查项:{}", checkItem);
        try {
            checkItemService.editCheckItem(checkItem);
            return new Result(true, "编辑成功");
        } catch (Exception e) {
            log.error("编辑检查项失败: {}", e.getMessage(), e);
            throw new RuntimeException("编辑检查项失败，请稍后重试");
        }
    }
    //GET /checkitem/findAll
    //查询所有检查项 用于回显
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("查询所有检查项");
        try{
            return new Result(true, "查询成功", checkItemService.findAll());
        }
        catch (Exception e){
            log.error("查询所有检查项失败: {}", e.getMessage(), e);
            throw new RuntimeException("查询所有检查项失败，请稍后重试");
        }
    }

    //GET /checkitem/findCheckItemIdsByCheckGroupId
    //根据检查组id查询检查项id
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(@RequestParam("checkgroupId") Integer id){
        log.info("根据检查组id查询检查项id:{}", id);
        try {
            return new Result(true, "查询成功", checkItemService.findCheckItemIdsByCheckGroupId(id));
        } catch (Exception e) {
            log.error("根据检查组id查询检查项id失败: {}", e.getMessage(), e);
            throw new RuntimeException("根据检查组id查询检查项id失败，请稍后重试");
        }
    }

}
